package top.eleven.sixpence.generaterollbacksql.convertor;


import top.eleven.sixpence.generaterollbacksql.annotation.Convertor;
import top.eleven.sixpence.generaterollbacksql.common.CommonConstant;
import top.eleven.sixpence.generaterollbacksql.common.SQLKey;
import top.eleven.sixpence.generaterollbacksql.common.util.StringUtils;
import top.eleven.sixpence.generaterollbacksql.exception.ConvertException;
import top.eleven.sixpence.generaterollbacksql.processor.ProcessorProperties;

@Convertor(sqlPrefix = {SQLKey.INSERT_INTO_C, SQLKey.INSERT_INTO_L})
public class InsertIntoConvertor implements BaseConvertor{


    @Override
    public String convert(String sql) {
        StringBuilder result = new StringBuilder(SQLKey.DELETE_FROM_C);
        //去除insert into关键字
        sql = sql.replace(SQLKey.INSERT_INTO_C, CommonConstant.EMPTY_STRING).replace(SQLKey.INSERT_INTO_L, CommonConstant.EMPTY_STRING).trim();

        //获取表名
        String table = sql.substring(0, sql.indexOf(" "));
        sql = sql.replace(table, CommonConstant.EMPTY_STRING);
        result.append(CommonConstant.ONE_BLANK).append(table).append(CommonConstant.ONE_BLANK);

        //区分字段名与值
        String[] values = sql.split(SQLKey.VALUES_C);
        values = values.length == 2 ? values : sql.split(SQLKey.VALUES_L);
        if(values.length != 2){
            throw new ConvertException("In SQL statement field name, table name, value maybe contains key word.");
        }
        String[] k = values[0].replace(CommonConstant.LEFT_BRACKET, CommonConstant.EMPTY_STRING)
                              .replace(CommonConstant.RIGHT_BRACKET, CommonConstant.EMPTY_STRING)
                              .split(CommonConstant.COMMA);
        String[] v = values[1].replace(CommonConstant.LEFT_BRACKET, CommonConstant.EMPTY_STRING)
                              .replace(CommonConstant.RIGHT_BRACKET, CommonConstant.EMPTY_STRING)
                              .replace(CommonConstant.SINGLE_QUOTATION_MARK,CommonConstant.EMPTY_STRING)
                              .split(CommonConstant.COMMA);
        if(k.length != v.length){
            throw new ConvertException("number of field not equals number of values");
        }

        //去除值前后空格
        for (int i = 0; i < v.length; i++) {
            v[i] = v[i].trim();
        }
        result.append(SQLKey.WHERE_C);
        int length = k.length;
        for (int i = 0; i < length; i++) {
            //如果值为 NULL null sysdate 忽略，不拼接
             if(ignoreValue(v[i])){
                 continue;
             }

            result.append(k[i]).append(CommonConstant.EQUALS)
                    .append(CommonConstant.SINGLE_QUOTATION_MARK)
                    .append(v[i])
                    .append(CommonConstant.SINGLE_QUOTATION_MARK);
            if (i < length - 1) {
                result.append(CommonConstant.ONE_BLANK)
                        .append(SQLKey.AND_C)
                        .append(CommonConstant.ONE_BLANK);
            }
        }
        return result.append(CommonConstant.SEMICOLON).append(CommonConstant.NEW_LINE).toString();
    }

    private boolean ignoreValue(String v) {
        if(StringUtils.isBlank(v)){
            return false;
        }
        String[] ignoreValues = ProcessorProperties.build().getIgnoreValues();
        for (String ignoreValue : ignoreValues) {
            if (ignoreValue.equals(v)) {
                return true;
            }
        }
        return false;
    }
}
